package com.heima.article.interceptor;

import com.heima.utils.thread.AppUserThreadLocalUnit;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ArticleInterceptor implements HandlerInterceptor {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       //清理threadLocal
        AppUserThreadLocalUnit.clear();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //得到文章id
        String userId = request.getHeader("userId");
        if (userId != null) {
            //存入ThreadLocal
            AppUserThreadLocalUnit.set(Integer.valueOf(userId));
        }
        return true;
    }
}
